/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.bean_nhanvien;
import Model.bean_setmatkhau;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author thien_000
 */
@WebServlet(name = "controller_nhanvien", urlPatterns = {"/controller_nhanvien"})
public class controller_nhanvien extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            try {
                String action = request.getParameter("btAction");
                if (action.equals("qlnv")) {
                    bean_nhanvien nv = new bean_nhanvien();
                    bean_nhanvien[] result = nv.hienthinhanvien();
                    request.setAttribute("INFO", result);
                    RequestDispatcher rd = request.getRequestDispatcher("QuanLyNhanVien.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Thêm mới")) {
                    RequestDispatcher rd = request.getRequestDispatcher("ThemNhanVien.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Thêm nhân viên")) {
                    String field_MaNV = request.getParameter("txtMaNV");
                    String field_TaiKhoan = request.getParameter("txtTaiKhoan");
                    String field_MatKhau = request.getParameter("txtMatKhau");
                    String field_HoTen = request.getParameter("txtHoTen");
                    String field_GioiTinh = request.getParameter("txtGioiTinh");
                    String field_ChucVu = request.getParameter("txtChucVu");
                    String field_SoDT = request.getParameter("txtSoDT");
                    bean_nhanvien nv = new bean_nhanvien();
                    String url = "controller_nhanvien?btAction=qlnv";
                    boolean result = nv.insert(field_MaNV, field_TaiKhoan, field_MatKhau, field_HoTen, field_GioiTinh, field_ChucVu, field_SoDT);
                    if (result) {
                        RequestDispatcher rd = request.getRequestDispatcher(url);
                        rd.forward(request, response);
                    }else{
                        RequestDispatcher rd = request.getRequestDispatcher("Fail_ThemNhanVien.jsp");
                    rd.forward(request, response);
                    }
                } else if (action.equals("Search")) {
                    String taikhoan = request.getParameter("txtSearch");
                    bean_nhanvien nv = new bean_nhanvien();
                    bean_nhanvien[] result = nv.searchLikeTaiKhoan(taikhoan);
                    request.setAttribute("INFO", result);
                    RequestDispatcher rd = request.getRequestDispatcher("QuanLyNhanVien.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Delete")) {
                    String taikhoan = request.getParameter("deleteTaiKhoan");
                    bean_nhanvien nv = new bean_nhanvien();
                    nv.setTaiKhoan(taikhoan);
                    boolean result = nv.deleteNhanVien();
                    String url = "controller_nhanvien?btAction=qlnv";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                } else if (action.equals("Update")) {
                    String taikhoan = request.getParameter("taikhoan");
                    String matkhau = request.getParameter("matkhau");
                    String hoten = request.getParameter("hoten");
                    String gioitinh = request.getParameter("gioitinh");
                    String chucvu = request.getParameter("chucvu");
                    String sodt = request.getParameter("sodt");
                    bean_nhanvien nv = new bean_nhanvien(taikhoan, matkhau, hoten, chucvu, gioitinh, sodt);
                    boolean result = nv.updateNhanVien();
                    String url = "controller_nhanvien?btAction=qlnv";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                } else if (action.equals("Xóa tất cả")) {
                    bean_nhanvien nv = new bean_nhanvien();
                    boolean result = nv.deleteAll();
                    String url = "controller_nhanvien?btAction=qlnv";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                }
                else if(action.equals("send_email"))
                {
                    bean_setmatkhau bt= new bean_setmatkhau();                    
                    String b=request.getParameter("txtemail");
                    
                    ResultSet rs= bt.gettaikhoan(b);
                    if(rs.next())
                    {
                    String matkhau=rs.getString("MatKhau");
                    String taikhoan=rs.getString("TaiKhoan");                   
                    bean_setmatkhau db=new bean_setmatkhau("kimcuc0202@gmail.com",b,"Quen Mat Khau Va Tai Khoan","Tài Khoản của bạn là: "+taikhoan+"  Mật khẩu: "+matkhau+"");
                    db.send();                  
                    request.setAttribute("ketqua", taikhoan);
                    RequestDispatcher dis=request.getRequestDispatcher("SetMatKhau.jsp");
                    dis.forward(request, response);
                    }
                   
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
